﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class web_pages_nhatuyendung_ketnoi : System.Web.UI.Page
{
    public ViecLamTrong vltcomm = new ViecLamTrong();
    public Common comm = new Common();
    public objViecLamTrong objVLT = new objViecLamTrong();
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "Nhà tuyển dụng " + ConfigurationManager.AppSettings["centername"];
        if (Session["NTDuser"] == null)
        {            
            Response.Redirect("nhatuyendung.aspx");

        }
        if (Session["NTDuser"].ToString().Equals("N"))
        {
            Response.Redirect("nhatuyendung.aspx");

        }
        string ma_dn = Session["NTDid"].ToString();
        string ma_ho_so = Request.QueryString["id"];
        objVLT = ViecLamTrong.getViecLamTrong(ma_ho_so);

    }
    protected void btn_tim_kiem_Click(object sender, EventArgs e)
    {
        if (chk_kn_mem.Checked == false && chk_nghiep_vu.Checked == false && chk_nguoi.Checked == false && chk_chuc_danh.Checked == false && chk_chuyen_mon.Checked == false && chk_ho_khau.Checked == false && chk_noi_lv.Checked == false && chk_thoi_gian.Checked == false)
        {
            Response.Write("<script> alert('Ít nhất một tiêu chí kết nối phải được chọn!') </script>");
        }
        else
        {

            string ma_ho_so = Request.QueryString["id"];
            objVLT = ViecLamTrong.getViecLamTrong(ma_ho_so);

            String SqlComm = "select A.chuc_danh_mong_muon, A.ngay_sinh, A.ma_nguoi,A.ma_ho_so,A.tieu_de,A.ho_ten,ten_chuc_danh from ( "
                + " SELECT NguoiTimViec_ho_so.chuc_danh_mong_muon, ConNguoi_nguoi.ngay_sinh, NguoiTimViec_ho_so.ma_nguoi,NguoiTimViec_ho_so.ma_ho_so,NguoiTimViec_ho_so.tieu_de,ConNguoi_nguoi.ho_ten from dbo.NguoiTimViec_ho_so "
                + " inner join dbo.ConNguoi_nguoi on NguoiTimViec_ho_so.ma_nguoi = ConNguoi_nguoi.ma_nguoi"
                + " where  NguoiTimViec_ho_so.kich_hoat = 1 ";
            if (chk_nguoi.Checked)
            {
                if (objVLT.listYeuCauVeNguoi.Count > 0)
                {
                    SqlComm += " AND ( (1<>1) ";
                    foreach (objYeuCauVeNguoi _obj in objVLT.listYeuCauVeNguoi)
                    {
                        SqlComm += " OR (";
                        if (_obj.gioi_tinh == "1") SqlComm += "  (gioi_tinh = 1) ";
                        else if (_obj.gioi_tinh == "1") SqlComm += "  (gioi_tinh = 0) ";
                        else SqlComm += "   (gioi_tinh = 0 or gioi_tinh=1) ";
                        SqlComm += " and chieu_cao >= " + _obj.cao_tu + " and chieu_cao <= " + _obj.cao_den + " ";
                        SqlComm += " and Year(getdate())-year(ngay_sinh) >=  " + _obj.tuoi_tu + " and Year(getdate())-year(ngay_sinh) <= " + _obj.tuoi_den + " ";
                        SqlComm += " ) ";
                    }
                    SqlComm += " ) ";
                }
            }
            if (chk_chuc_danh.Checked) SqlComm += " and chuc_danh_mong_muon = '" + objVLT.ma_chuc_danh + "' ";
            if (chk_thoi_gian.Checked) SqlComm += " and NguoiTimViec_ho_so.ma_ho_so in (select ma_ho_so from NguoiTimViec_thoi_gian_lam_viec where ma_thoi_gian_lv = '" + objVLT.ma_thoi_gian + "')";

            string[] noilamviec = new string[] { };
            if (chk_noi_lv.Checked && objVLT.noi_lam_viec.Length > 1)
            {
                noilamviec = objVLT.noi_lam_viec.Split(new char[] { '#' });
                if (noilamviec.Length > 0)
                {
                    SqlComm += " AND (";
                    for (int i = 0; i < noilamviec.Length - 1; i++)
                    {
                        if (i == 0) SqlComm += "  NguoiTimViec_ho_so.ma_ho_so in (select ma_ho_so from NguoiTimViec_thanh_pho_lam_viec where ma_dia_chi = '" + noilamviec[i] + "')";
                        else if (i != 0) SqlComm += " OR  NguoiTimViec_ho_so.ma_ho_so in (select ma_ho_so from NguoiTimViec_thanh_pho_lam_viec where ma_dia_chi = '" + noilamviec[i] + "')";
                    }
                    SqlComm += " ) ";
                }

            }

            string[] hokhau = new string[] { };
            if (chk_ho_khau.Checked && objVLT.ho_khau.Length > 1)
            {
                hokhau = objVLT.ho_khau.Split(new char[] { '@' });
                if (hokhau.Length > 1)
                {
                    SqlComm += " AND ( ";
                    for (int i = 0; i < hokhau.Length - 1; i++)
                    {
                        if (i == 0) SqlComm += " connguoi_nguoi.ho_khau like '" + hokhau[i].Trim() + "%'  ";
                        else if (i != 0) SqlComm += " OR connguoi_nguoi.ho_khau like '" + hokhau[i].Trim() + "%' ";
                    }
                    SqlComm += " ) ";
                }

            }

            string[] khanangmem = new string[] { };
            if (chk_kn_mem.Checked && objVLT.listYeuCauKhaNangMem.Count > 0)
            {


                SqlComm += " AND ( (1<>1) ";
                foreach (objYeuCauKhaNangMem _obj in objVLT.listYeuCauKhaNangMem)
                {
                    SqlComm += " OR  conNguoi_nguoi.ma_nguoi in (select ma_nguoi from conNguoi_kha_nang_mem where ma_kn_mem = '" + _obj.ma_kn_mem + "' )  ";
                }

                SqlComm += " ) ";


            }
            string[] nghiepvu = new string[] { };
            if (chk_nghiep_vu.Checked && objVLT.listYeuCauNghiepVu.Count > 0)
            {

                SqlComm += " AND ( (1<>1) ";
                foreach (objYeuCauNghiepVu _obj in objVLT.listYeuCauNghiepVu)
                {
                    SqlComm += " OR conNguoi_nguoi.ma_nguoi in (select ma_nguoi from conNguoi_nghiep_vu where ma_nghiep_vu = '" + _obj.ma_nghiep_vu + "' )  ";
                }

                SqlComm += " ) ";



            }

            if (chk_chuyen_mon.Checked && objVLT.listYeuCauChuyenMon.Count > 0)
            {
                SqlComm += " AND ( (1<>1) ";
                foreach (objYeuCauChuyenMon _obj in objVLT.listYeuCauChuyenMon)
                {
                    SqlComm += " OR NguoiTimViec_ho_so.ma_nguoi in (select ma_nguoi from ConNguoi_hoc_van_chuyen_mon where ma_chuyen_mon like '" + _obj.ma_chuyen_mon + "%' and ma_hoc_van = '" + _obj.ma_hoc_van + "' and kinh_nghiem >= " + _obj.kinh_nghiem + ") ";
                }

                SqlComm += " ) ";

            }
            SqlComm += " ) as A "
            + " inner join DanhMuc_chuc_danh on chuc_danh_mong_muon = ma_chuc_danh";
            //  Response.Write(SqlComm);
            FillData(SqlComm);
        }
    }
    void FillData(string sql)
    {
        rpt_result.DataSource = comm.getDataTable(sql);
        rpt_result.DataBind();
    }
}
